# Contributor: Andrei Jiroh Eugenio Halili <ajhalili2006@andreijiroh.eu.org>
# Contributor: Robert Günzler <r@gnzler.io>
# Contributor: Xe <xe@tailscale.com>
# Maintainer: Robert Günzler <r@gnzler.io>
pkgname=tailscale
pkgver=1.62.0
pkgrel=2
pkgdesc="The easiest, most secure way to use WireGuard and 2FA"
url="https://tailscale.com/"
license="BSD-3-Clause"
arch="all !s390x" # upstream still doesn't have hardware to debug this
options="chmod-clean"
depends="iptables" # https://github.com/tailscale/tailscale/issues/391
makedepends="go linux-headers"
checkdepends="helm iproute2-minimal openssh"
subpackages="$pkgname-doc $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/tailscale/tailscale/archive/v$pkgver.tar.gz
	tailscale.confd
	tailscale.initd
	tailscale.logrotate
	tailscale.modules-load
	test-bump-conn-max-overhead.patch
	"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

# secfixes:
#   1.32.3-r0:
#     - CVE-2022-41924 TS-2022-004
#   0:
#     - CVE-2022-41925 TS-2022-005

prepare() {
	default_prepare

	# this script attempts to download a helm binary
	# use the helm we have in repo instead
	rm -v tool/helm
	ln -sv /usr/bin/helm tool/helm
}

build() {
	msg2 "Building tailscale..."

	TAGS="netcgo osusergo static_build xversion"
	VERSIONFLAGS="
	-X tailscale.com/version.shortStamp=$pkgver
	-X tailscale.com/version.longStamp=$pkgver-AlpineLinux
	-X tailscale.com/version.gitCommitStamp=AlpineLinux
	"

	go build -tags "$TAGS" -ldflags "$VERSIONFLAGS" ./cmd/tailscale
	go build -tags "$TAGS" -ldflags "$VERSIONFLAGS" ./cmd/tailscaled
}

check() {
	# shellcheck disable=SC2046
	go test $(go list ./... | grep -Ev '(util/linuxfw|net/netcheck|wgengine/magicsock|tstest/archtest|tstest/integration|tstest/jsdeps|tstest/iosdeps|ssh/tailssh|containerboot|net/connstats)')
}

package() {
	install -m755 -D tailscale \
		"$pkgdir"/usr/bin/tailscale
	install -m755 -D tailscaled \
		"$pkgdir"/usr/sbin/tailscaled

	install -m644 -D -t "$pkgdir"/usr/share/doc/$pkgname README.md

	install -m755 -D "$srcdir"/tailscale.initd "$pkgdir"/etc/init.d/tailscale
	install -m644 -D "$srcdir"/tailscale.confd "$pkgdir"/etc/conf.d/tailscale
	install -m644 -D "$srcdir"/tailscale.logrotate "$pkgdir"/etc/logrotate.d/tailscale
	install -m644 -D "$srcdir"/tailscale.modules-load "$pkgdir"/usr/lib/modules-load.d/tailscale.conf
}

sha512sums="
187cadb3eb0db54b8b4a6ae78d77745750167ead7fd6e68bc95171ea6058386e91696a3e36fdd8101f8a52fe0a2d9344484b4bca0a48929f19b2d314bb182e1c  tailscale-1.62.0.tar.gz
24706f10a6f5f7091c0f73a13e0c2d72e969e92e6f7aa21abe504f8e2c16af4d1d80fd28c33acbe4866dd8174abd9dc201d97eb5271f6355f7589a56f470f52c  tailscale.confd
5cb35e0144d3bc2c20b1fde1ee402aab642b666c259bb324bf1c742a79fd8b3892cf2e7419e684764a50b6a29dc562ee674915bbf16a76eda543fd9c8988428c  tailscale.initd
4f44f59553142bff5ca0914543114e4a6e3e413a96353543551445be6fa3ddc1037492baf0e091c95fe22a63e639b58bc5040285b8b5d246dbb57b70337d5178  tailscale.logrotate
871b650ff982be061532b4c5fe4812f41e6e2c58fc69b24f8a745f9a43389da44e742a69b0467b3c3d9e2b031af0728e20f10fa4584695c4f5ac87768a1fd34e  tailscale.modules-load
d62ca3f75f23319ffb45452499d082a82862ba90099e50287cc152ba6d9796bc5c4ad2a90d662919023e0a6e9b1e7722d8f35ba85f4807908abcc202d77fc31c  test-bump-conn-max-overhead.patch
"
